{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `std::generate`\n",
    "\n",
    "{func}`std::generate` 是 C++ 标准库中的一个算法，用于生成序列。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "比如，获取零元素列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "vscode": {
     "languageId": "c++"
    }
   },
   "outputs": [],
   "source": [
    "const std::vector<float> p {2, 3, 4, 5, 0 ,2, 0};\n",
    "std::vector<size_t> is_zeros(p.size());\n",
    "auto it = p.begin();\n",
    "std::generate(is_zeros.begin(), is_zeros.end(),\n",
    "                [&it]() { return static_cast<size_t>(*(it++) == 0.f); });"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "vscode": {
     "languageId": "c++"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ 0, 0, 0, 0, 1, 0, 1 }"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "is_zeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "c++"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "C++14",
   "version": "14"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
